<?php
include_once(dirname(__FILE__) . "/../../configure/configure.php");		//--> global var
include_once(dirname(__FILE__) . "/../configure/admin.config.inc.php");	//--> admin global var
include_once(dirname(__FILE__) . "/../tools/smarty/libs/Smarty.class.php");					//--> out template

require_once(dirname(__FILE__) . "/../controlHeader.php");					//--> system control header
require_once(dirname(__FILE__) . "/../controlOperate.php");				//-->

include_once("impl/ContentImpl.php");
include_once("impl/CategoryImpl.php");
include_once("impl/CelebrityImpl.php");
include_once("impl/TagImpl.php");

	
$contentImpl= new ContentImpl(DB_TAG_MM);
$oCatImpl 	= new CategoryImpl(DB_TAG_MM);
$oCeleImpl 	= new CelebrityImpl(DB_TAG_MM);
$tagImpl	= new TagImpl(DB_TAG_MM);


$error_message 	= '';

$name		= '';
$summary	= '';
$status		= '1';
$recommend	= '0';

$dto = $contentImpl->getById($id);

if(isset($_POST['Submit']) && is_object($dto)){
	$name			= trim($_POST['name']);
	$tags			= trim($_POST['tags']);
	
	$comments 		= trim($_POST['comments']);
	$description	= trim($_POST['description']);
	$status			= $_POST['status'];
	$recommend		= $_POST['recommend'];
	
	$catIds			= $_POST['catIds'];// array
	$celebrityId	= $_POST['celebrityId'];
	
	$imageCount 	= $contentImpl->getImageCount($dto->id);
		
	$dto->name 			= $name;
	$dto->comments 		= $comments;
	$dto->description	= $description;
	$dto->status		= $status;
	$dto->recommend		= $recommend;
	
	$dto->r_imageCount	= $imageCount;
		
	$isOk = $contentImpl->edit($dto);
	if($isOk){
		$contentImpl->addCategories($id, $catIds);
		
		$contentImpl->delCelebrity($id);
		if(!empty($celebrityId)){
			$contentImpl->addCelebrity($id, $celebrityId);
		}
		
		$contentImpl->delTags($id);
		$tagIds = $tagImpl->addTags($tags);
		foreach($tagIds as $tagId)
			$contentImpl->addTag($id, $tagId);
		
		$error_message = 'edit succeed.';
		
		echo "<script language='javascript'>";
		echo "alert(\"$error_message\");";
		echo "location.href=\"$backurl\";";
		echo "</script>";
	}else 
		$error_message = 'edit failure.';

}


$catIds 		= $contentImpl->getCategoryIds($dto->id);
$catLeaf 		= $oCatImpl->getLeafList();

$celebrityId 	= $contentImpl->getCelebrityId($dto->id);
$arrCelebrity	= $oCeleImpl->getList();

$tags 			= $contentImpl->getTags($dto->id);
$dto->tags = $tags;

$arrStatus 		= array('1' => 'Online', '0' => 'Offline');
$arrRecommend 	= array('1' => 'Yes', '0' => 'No');


/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;

$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);
$smarty->assign('error_message', $error_message);

$smarty->assign('arrStatus', $arrStatus);
$smarty->assign('arrRecommend', $arrRecommend);

$smarty->assign('id', $id);
$smarty->assign('dto', $dto);

$smarty->assign('catLeaf', $catLeaf);
$smarty->assign('catIds', $catIds);

$smarty->assign('arrCelebrity', $arrCelebrity);
$smarty->assign('celebrityId', $celebrityId);

$smarty->display('listContent_Edit.htm');
?>
